<script setup lang="ts" name="PaneCandidateStarter">
import { mapState } from 'pinia'
import { useModelerStore } from '@/store/bpmnProcess/modelerStore'
import EventEmitter from '@/utils/EventEmitter'
import { isCandidateStarter, getCandidateStarterGroup, setCandidateStarterGroup, getCandidateStarterUsers, setCandidateStarterUsers } from '../../utils/candidateStarter'

const mapStates = computed(() => {
  return mapState(useModelerStore, ['getActive'])
})

const form = ref({
  group: '',
  user: ''
})

const getGroup = () => {
  form.value.group = getCandidateStarterGroup(mapStates.value.getActive()) || ''
}
const setGroup = (v) => {
  setCandidateStarterGroup(mapStates.value.getActive(), v)
}

const getUser = () => {
  form.value.user = getCandidateStarterUsers(mapStates.value.getActive()) || ''
}
const setUser = (v) => {
  setCandidateStarterUsers(mapStates.value.getActive(), v)
}

const initData = () => {
  getGroup()
  getUser()
}

const eventEmitterListener = () => {
  if (isCandidateStarter(mapStates.value.getActive())) {
    initData()
  }
}

onBeforeUnmount(() => {
  EventEmitter.removeListener('element-update', eventEmitterListener)
})

onMounted(() => {
  initData()
  EventEmitter.on('element-update', eventEmitterListener)
})
</script>

<template>
  <el-collapse-item name="candidate-starter">
    <template #title>
      <div class="collapse-header">
        <svg-icon icon="ri:terminal-box-fill"></svg-icon>
        <h2>候选启动器</h2>
      </div>
    </template>
    <el-form-item label="候选组">
      <el-input v-model="form.group" @change="setGroup" />
    </el-form-item>
    <el-form-item label="候选用户">
      <el-input v-model="form.user" @change="setUser" />
    </el-form-item>
  </el-collapse-item>
</template>

<style scoped lang="scss">
.el-collapse-item {
  :deep(.el-collapse-item__header) {

    .collapse-header {
      display: flex;
      flex: 1;
      align-items: center;
      padding: 0 12px;

      .el-icon {
        margin-right: 6px;
        font-size: 20px !important;
      }

      h2 {
        font-weight: bold;
        font-size: 14px;
      }
    }

    .el-icon {
      font-size: 18px;
    }
  }

  :deep(.el-collapse-item__content) {
    padding-right: 12px;
    padding-bottom: 12px;
    padding-left: 12px;
  }
}
</style>
